package com.thd.framework.camunda.controller;

import com.thd.framework.camunda.entity.ProcessInstanceTaskEntity;
import com.thd.framework.camunda.service.WorkFlowService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * com.thd.springboottest.camunda.controller.CamundaController
 *
 * @author: wanglei62
 * @DATE: 2022/5/26 17:01
 **/
@Controller
@RequestMapping("/camunda")
public class CamundaController {
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private WorkFlowService workFlowService;

    @RequestMapping(value="/getAllService",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> getAllService(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();//一直往下debug
        System.out.println("流程名称：" + processEngine.getName());
        System.out.println("配置类：" + processEngine.getProcessEngineConfiguration());
        System.out.println("鉴权：" + processEngine.getAuthorizationService());
        System.out.println("外部任务：" + processEngine.getExternalTaskService());
        System.out.println("过滤器：" + processEngine.getFilterService());

        System.out.println(processEngine.getFormService());
        System.out.println(processEngine.getHistoryService());

        System.out.println(processEngine.getIdentityService());
        System.out.println(processEngine.getManagementService());
        System.out.println(processEngine.getRepositoryService());
        System.out.println(processEngine.getRuntimeService());
        System.out.println(processEngine.getTaskService());
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping("/queryTaskByBusinessKey/{businessKey}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryTaskByBusinessKey/normal001
    public ResponseEntity<List<ProcessInstanceTaskEntity>> queryTaskByBusinessKey(@PathVariable String businessKey){
        return ResponseEntity.ok(this.workFlowService.queryTaskByBusinessKey(businessKey));
    }

    @RequestMapping("/queryTaskForUser/{userId}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryTaskForUser/[userId]
    public ResponseEntity<List<ProcessInstanceTaskEntity>> queryTaskForUser(@PathVariable String userId){
        return ResponseEntity.ok(this.workFlowService.queryTaskForUser(userId));
    }

    @RequestMapping("/deleteDeployCascade/{deployId}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/deleteDeployCascade/[deployId]
    public ResponseEntity<String> deleteDeployCascade(@PathVariable String deployId){
        this.workFlowService.deleteDeployCascade(deployId);
        return ResponseEntity.ok("SUCCESS");
    }


    @RequestMapping("/completeTask/{taskId}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/completeTask/[taskId]
    public ResponseEntity<String> completeTask(@PathVariable String taskId){
        this.workFlowService.completeTask(taskId,null);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/completeTaskWithVariable/{taskId}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/completeTaskWithVariable/[taskId]
    public ResponseEntity<String> completeTaskWithVariable(@PathVariable String taskId, @RequestBody Map variable){
        this.workFlowService.completeTask(taskId,variable);
        return ResponseEntity.ok("SUCCESS");
    }


    @RequestMapping(value="/isEndedByBussinessKey/{businessKey}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/isEndedByBussinessKey/[businessKey]
    public ResponseEntity<Boolean> isEndedByBussinessKey(@PathVariable String businessKey){
        boolean isClose = this.workFlowService.isEndedByBussinessKey(businessKey);
        return ResponseEntity.ok(isClose);
    }



    @RequestMapping(value="/addVariableForProcessInstance/{processInstanceId}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance
    public ResponseEntity<String> addVariableForProcessInstance(@PathVariable String processInstanceId ,@RequestBody Map variable){
        this.workFlowService.addVariableForProcessInstance(processInstanceId,variable);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/addVariableForProcessInstanceByBusinessKey/{businessKey}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/addVariableForProcessInstance
    public ResponseEntity<String> addVariableForProcessInstanceByBusinessKey(@PathVariable String businessKey ,@RequestBody Map variable){
        this.workFlowService.addVariableForProcessInstanceByBusinessKey(businessKey,variable);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/addVariableForExecutionLocal/{executionId}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/addVariableForExecutionLocal
    public ResponseEntity<String> addVariableForExecutionLocal(@PathVariable String executionId ,@RequestBody Map variable){
        this.workFlowService.addVariableForExecutionLocal(executionId,variable);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/addVariableForExecution/{executionId}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/addVariableForExecution
    public ResponseEntity<String> addVariableForExecution(@PathVariable String executionId ,@RequestBody Map variable){
        this.workFlowService.addVariableForExecution(executionId,variable);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/addVariableForTask/{taskId}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/addVariableForTask
    public ResponseEntity<String> addVariableForTask(@PathVariable String taskId ,@RequestBody Map variable){
        this.workFlowService.addVariableForTask(taskId,variable);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/addVariableForTaskLocal/{taskId}",method=RequestMethod.POST)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/addVariableForTaskLocal
    public ResponseEntity<String> addVariableForTaskLocal(@PathVariable String taskId ,@RequestBody Map variable){
        this.workFlowService.addVariableForTaskLocal(taskId,variable);
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value="/queryVariableForProcessInstance/{processInstanceId}",method=RequestMethod.GET)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryVariableForProcessInstance
    public ResponseEntity<Map> queryVariableForProcessInstance(@PathVariable String processInstanceId ){
        Map map = this.workFlowService.queryVariableForProcessInstance(processInstanceId);
        return ResponseEntity.ok(map);
    }

    @RequestMapping(value="/queryVariableForProcessInstanceByBusinessKey/{businessKey}",method=RequestMethod.GET)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryVariableForProcessInstanceByBusinessKey
    public ResponseEntity<Map> queryVariableForProcessInstanceByBusinessKey(@PathVariable String businessKey ){
        Map map = this.workFlowService.queryVariableForProcessInstanceByBusinessKey(businessKey);
        return ResponseEntity.ok(map);
    }

    @RequestMapping(value="/queryVariableForExecution/{executionId}",method=RequestMethod.GET)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryVariableForExecution
    public ResponseEntity<Map> queryVariableForExecution(@PathVariable String executionId ){
        Map map = this.workFlowService.queryVariableForExecution(executionId);
        return ResponseEntity.ok(map);
    }

    @RequestMapping(value="/queryVariableForExecutionLocal/{executionId}",method=RequestMethod.GET)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryVariableForExecutionLocal
    public ResponseEntity<Map> queryVariableForExecutionLocal(@PathVariable String executionId ){
        Map map = this.workFlowService.queryVariableForExecutionLocal(executionId);
        return ResponseEntity.ok(map);
    }

    @RequestMapping(value="/queryVariableForTask/{taskId}",method=RequestMethod.GET)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryVariableForTask
    public ResponseEntity<Map> queryVariableForTask(@PathVariable String taskId ){
        Map map = this.workFlowService.queryVariableForTask(taskId);
        return ResponseEntity.ok(map);
    }

    @RequestMapping(value="/queryVariableForTaskLocal/{taskId}",method=RequestMethod.GET)
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/camunda/queryVariableForTaskLocal
    public ResponseEntity<Map> queryVariableForTaskLocal(@PathVariable String taskId ){
        Map map = this.workFlowService.queryVariableForTaskLocal(taskId);
        return ResponseEntity.ok(map);
    }


    /**
     * 自由流
     * @param processInstanceId
     * @param activitiId
     * @return
     */
    @RequestMapping(value="/jump/{processInstanceId}/{activitiId}",method=RequestMethod.GET)
    // url : http://127.0.0.1:8899/thd/camunda/jump/xxx/xxxx
    public ResponseEntity<String> jump(@PathVariable String processInstanceId,@PathVariable String activitiId){
        workFlowService.jump(processInstanceId,activitiId);
        return ResponseEntity.ok("SUCCESS");
    }












}
